<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: AddUserFavorite.php
 * Date: 2017/1/10
 * Time: 0:48
 */
namespace app\api\model\page\user;

use app\api\model\dao\Goods;
use app\library\Error;
use app\library\PageBase;
use app\api\model\dao\UserFavorite;
use app\api\model\page\note\GetNoteInfo;
use think\Log;
use think\Validate;

class AddUserFavorite extends PageBase
{
    public function __construct()
    {
        parent::__construct();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        $ret = $this->checkParam($param);
        if (true !== $ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        unset($ret);

        $favoriteData = [
            //'user_id' => $param['user_id'],
            'user_id' => $this->user_id,
            'fav_type' => $param['fav_type'],
            'fav_id' => $param['fav_id'],
            'status' => 1, //1收藏，0删除
        ];
        $objFavorite = new UserFavorite();
        //判断是否已经收藏
        $ret = $objFavorite->where($favoriteData)->select();
        if (count($ret)) {
            return $this->getRes(Error::ERROR_ADD_FAVORITE_ALREADY_EXIST);
        }
        //根据fav_type选择从帖子或者从商品获取详情
        if (1 == $param['fav_type']) {
            //商品
            $objGoods = new Goods();
            $goodsInfo = $objGoods->getGoods($param['fav_id']);
            if (false == $goodsInfo) {
                return $this->getRes(Error::ERROR_ADD_FAVORITE_NOT_EXIST);
            }
            $favoriteData['title'] = $goodsInfo[0]['goods_name'];
            $favoriteData['description'] = $goodsInfo[0]['shop_price'];
        }
        if (2 == $param['fav_type']) {
            //帖子
            $noteInfo = $this->getNoteInfo($param['fav_id']);
            //var_dump($noteInfo);
            if (false == $noteInfo) {
                return $this->getRes(Error::ERROR_ADD_FAVORITE_NOT_EXIST);
            }
            $favoriteData['title'] = $noteInfo['title'];
            $favoriteData['description'] = $noteInfo['content'];
        }

        $ret = $objFavorite->addFavite($favoriteData);
        if (false === $ret) {
            Log::error("add favorite failed " . json_encode($favoriteData));
            return $this->getRes(Error::ERR_SYS);
        }
        return $this->getRes();
    }

    public function checkParam($param)
    {
        $rule = [
            //'user_id' => 'require',
            'fav_type' => 'require|in:1,2',
            'fav_id' => 'require',
        ];
        $msg = [
            //'user_id.require' => 'user_id必须',
            'fav_type.require' => 'fav_type',
            'fav_id.require' => 'fav_id必须',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return $result;

    }

    public function getGoodsInfo($id)
    {
        /*商品返回的价格，图片，名称，销量*/

    }

    public function getNoteInfo($id)
    {
        /*帖子返回，title,描述，作者，作者头像,评论和点赞数。*/
        $objNoteInfo = new GetNoteInfo();
        return $objNoteInfo->getNoteInfoFromDB($id);
    }
}
